package com.arch.dev.client.view.ui;

import java.util.List;
import java.util.Set;

import com.arch.dev.client.vo.UserVO;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
import com.google.gwt.view.client.SelectionModel;

public class UsersTable extends Composite{

	private static UsersTableUiBinder uiBinder = GWT
			.create(UsersTableUiBinder.class);

	interface UsersTableUiBinder extends UiBinder<Widget, UsersTable> {
	}
	
	@UiField(provided = true)
	CellTable<UserVO> cellTable;

	@UiField(provided = true)
	BudgetToolBar budgetToolBar;

	@UiField(provided = true)
	SimplePager pager;

	Widget view;

	ListDataProvider<UserVO> dataProvider;

	public UsersTable() {

		onInitialize();

		initWidget(uiBinder.createAndBindUi(this));

	}

	public UsersTable(Widget view) {

		this();

		this.view = view;

	}

	SelectionModel<UserVO> selectionModel;

	
	
	public void onInitialize() {	
		

		BudgetImageBundle bundle = (BudgetImageBundle) GWT

				.create(BudgetImageBundle.class);

		budgetToolBar = new BudgetToolBar();

		budgetToolBar.removeAllWidgets();
		
		
		cellTable = new CellTable<UserVO>();

		cellTable.setWidth("100%", true);

		// Add a selection model so we can select cells.

		selectionModel = new MultiSelectionModel<UserVO>(

		UserVO.KEY_PROVIDER);

		cellTable

		.setSelectionModel(selectionModel, DefaultSelectionEventManager

		.<UserVO> createCheckboxManager());

		// Create a Pager to control the table.

		SimplePager.Resources pagerResources = GWT

		.create(SimplePager.Resources.class);

		pager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0,

		true);

		pager.setDisplay(cellTable);

		// Initialize the columns.

		initTableColumns();

		cellTable.setPageSize(10);

		dataProvider = new ListDataProvider<UserVO>();

		// Connect the table to the data provider.

		dataProvider.addDataDisplay(cellTable);

	}

	/**
	 * 
	 * Add the columns to the table.
	 */

	private void initTableColumns() {

		// Create name column.

		TextColumn<UserVO> nameColumn = new TextColumn<UserVO>() {

			@Override
			public String getValue(UserVO contact) {

				return contact.getUserName();

			}

		};

		nameColumn.setSortable(true);


		TextColumn<UserVO> authorityColumn = new TextColumn<UserVO>() {
			@Override
			public String getValue(UserVO contact) {
				
				String auth = contact.getAuthorityVO().getAuthority();
				
				if(auth.equals("ROLE_ADMIN")){
					return "Administrator";
				}else if(auth.equals("ROLE_USER")){
					return "User";
				}else{
					return "";
				}		

			}

		};
		

		Column<UserVO, Boolean> checkColumn = new Column<UserVO, Boolean>(

		new CheckboxCell(true, false)) {

			@Override
			public Boolean getValue(UserVO object) {

				return selectionModel.isSelected(object);

			}

		};

		cellTable.setColumnWidth(checkColumn, 40, Unit.PX);

		cellTable.addColumn(checkColumn, SafeHtmlUtils

		.fromSafeConstant("<br/>"));

		cellTable.addColumn(nameColumn, "User Name");

		cellTable.addColumn(authorityColumn, "Authority");


	}

	public void populateTable(List<UserVO> UserVOs) {

		List<UserVO> list = dataProvider.getList();

		list.clear();

		for (UserVO UserVO : UserVOs) {

			list.add(UserVO);

		}

	}

	class BudgetSelectionChangeHandler implements Handler {

		
		public void onSelectionChange(SelectionChangeEvent event) {

			MultiSelectionModel<UserVO> selectionModel = (MultiSelectionModel<UserVO>) event

			.getSource();

			Set<UserVO> selected = selectionModel.getSelectedSet();

		}

	}

	public MultiSelectionModel<UserVO> getMultiSelectionModel() {
		return (MultiSelectionModel<UserVO>) selectionModel;
	}
	
	public void addToolBarWidget(Widget widget){
		budgetToolBar.addToolWidget(widget);
	}

}
